package com.project.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/9/7 17:10
 */
@Configuration
public class SysConfig {

    @Autowired
    CachingConnectionFactory connectionFactory;

    @Bean
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }

    //死信队列
    @Bean
    public Queue queueDead(){
        return new Queue("queueDead");
    }

    @Bean
    public Queue meituanQueue(){
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "directDeadExchange");
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key","deadKEY");
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",5000);
//        return new Queue("meituanQueue");
        return QueueBuilder.durable("meituanQueue").withArguments(args).build();
    }

    //死信交换机
    @Bean
    public DirectExchange directDeadExchange(){
        return new DirectExchange("directDeadExchange");
    }

    @Bean
    public DirectExchange meituanDirect(){
        return new DirectExchange("meituanDirect");
    }

    @Bean
    public Binding queueDeadTodirectDeadExchange(Queue queueDead,DirectExchange directDeadExchange){
        return BindingBuilder.bind(queueDead).to(directDeadExchange).with("deadKEY");
    }

    @Bean
    public Binding queueOneTodirectExchange(Queue meituanQueue, DirectExchange meituanDirect){
        return BindingBuilder.bind(meituanQueue).to(meituanDirect).with("meituan.Direct");
    }
}
